$(function () {
  //定义页码
  let cPage = 1;
  allComment();
  function allComment() {
    $.ajax({
      method: 'get',
      url: BigNew.comment_search,
      data: {
        page: cPage,
        perpage: 20,
      },
      success: (res) => {
        console.log(res);
        const htmlStr = template('comment_list', res.data);
        $('tbody').html(htmlStr);

        /*  for (let i = 0; i < res.data.totalCount; i++) {
           $("#audit").parent().parent().addClass('danger');
         } */

        setPage(cPage, res.data.totalPage);
      }
    })
  }


  //分页
  /**
       *
       * @param pageCurrent 当前所在页
       * @param pageSum 总页数
       * @param callback 调用ajax
       */
  function setPage(pageCurrent, pageSum, callback) {
    $("#pagination").bootstrapPaginator({
      //设置版本号
      bootstrapMajorVersion: 3,
      // 显示第几页
      currentPage: pageCurrent,
      // 总页数
      totalPages: pageSum,
      //当单击操作按钮的时候, 执行该函数, 调用ajax渲染页面
      onPageClicked: function (event, originalEvent, type, page) {
        // 把当前点击的页码赋值给currentPage, 调用ajax,渲染页面
        cPage = page;
        allComment && allComment();
      }
    })
  }


  //批准功能
  $('tbody').on('click', '#audit', function () {
    const id = $(this).attr('data-id');
    //console.log(id);
    $.ajax({
      method: 'post',
      url: BigNew.comment_pass,
      data: { id },
      success: (res) => {
        //console.log(res);
        // alert(res.msg);
        allComment();
      }
    })
  });

  //拒绝功能
  $('tbody').on('click', '#refuse', function () {
    const id = $(this).attr('data-id');
    console.log(id);
    $.ajax({
      method: 'post',
      url: BigNew.comment_reject,
      data: { id },
      success: (res) => {
        //console.log(res);
        // alert(res.msg);
        allComment();
      }
    })
  });



  //删除评论
  $('tbody').on('click', '#del', function () {
    const id = $(this).attr('data-id');
    console.log(id);
    $.ajax({
      method: 'post',
      url: BigNew.comment_delete,
      data: { id },
      success: (res) => {
        //console.log(res);
        // alert(res.msg);
        allComment();
      }
    })
  });

})